package com.aaa.Controller;

import com.aaa.Entity.Menu;
import com.aaa.Entity.User;
import com.aaa.Service.MenuService;
import com.aaa.Service.UserService;
import com.aaa.util.DefaultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    //跳转到登录界面
    @RequestMapping("/toLogin")
    public String login(){
        return "login";
    }

    //验证账号密码是否正确
    @RequestMapping("/checkUser")
    @ResponseBody
    public DefaultMsg checkUser(User user, HttpSession session){
        DefaultMsg defaultMsg = new DefaultMsg();
        User uAndP = userService.findUAndP(user);
        if(uAndP==null){
            defaultMsg.setError("账号或密码错误");
            return defaultMsg;
        }else {
            //往session中放置用户对象
            session.setAttribute("SESSION_USER",uAndP);
            return defaultMsg;
        }
    }


    /**
     * 查询用户能操作的所有菜单
     * @return
     */
    @RequestMapping("/listMiniUiMenus")
    @ResponseBody
    public Map<String, Object> listMiniUiMenus(HttpSession session){
        User  user =(User) session.getAttribute("SESSION_USER");
        //获取用户id 根据用户id 查询用户能操作的所有菜单
        Map<String, Object> map = menuService.listMiniUiMenus(user.getId());
        String a= "1";
        return map;
    }

    /**
     * 查询用户能操作的所有菜单
     * @return
     */
    @RequestMapping("/listMyMenus")
    @ResponseBody
    public List<Menu> listMyMenus(HttpSession session){
        User  user =(User) session.getAttribute("SESSION_USER");
        //获取用户id 根据用户id 查询用户能操作的所有菜单
        List<Menu> menus = menuService.listMyMenus(user.getId());
        return menus;
    }


    //跳转到主界面
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}
